home *** CD-ROM | disk | FTP | other *** search
/ Beginning Mac Programming / Beginning Mac Programming.bin / Open Me for REALbasic 3 / REALbasic 3.2 / Example Projects / Reusable Classes_Code / String_IO_Manipulation / EvalExpression 1.3 / Read Me < prev   
Text File  |  1999-11-19  |  4KB  |  135 lines

  1. EvalExpression 1.3
  2. ©1999 Jim Rodovich, Channel 8 Software
  3.  
  4. About EvalExpression
  5.  
  6. EvalExpression is a method which accepts a string and returns a double.  The string passed to it can consist of numbers and the following mathematical operands:
  7.   +  adds one number to another
  8.   -  subtracts one number from another
  9.   *  multiplies one number to another
  10.   /  divides one number by another
  11.   %  returns the remainder of one number by another
  12.   ^  raises one number to the power of another
  13.   ±  returns the negative of a number
  14.   ( and ) forces certain operations to be evaluated first.
  15.   _fcn(x) calls a function in the EvalFunction method (see below)
  16.  
  17. EvalExpression and the Order of Operations
  18.  
  19. EvalExpression uses the order of operations, as follows:
  20.  
  21. 1.  Operations in parenthesis are evaluated first
  22. 2.  Negative numbers (±) are evaluated
  23. 3.  Exponents are evaluated from left to right
  24. 4.  Multiplication, Division, and Remainders are evaluated from left to right
  25. 5.  Addition and Subtraction are evaluated from left to right.
  26.  
  27. Contacting the Author of EvalExpression
  28.  
  29. Hotline (at cafe.realbasic.com):
  30.    dzhim? [jim]
  31. E-Mail:
  32.    dzhim@home.com
  33. Internet:
  34.    http://members.home.net/dzhim/
  35.  
  36. EvalExpression Sample Code
  37.  
  38. Dim Expression as String, Result as Double
  39.  
  40. Expression = "-2"
  41. Result = EvalExpression(Expression)
  42. // Result = -2
  43.  
  44. Expression = "6+3"
  45. Result = EvalExpression(Expression)
  46. // Result = 9
  47.  
  48. Expression = "7-2*3"
  49. Result = EvalExpression(Expression)
  50. // Result = 1
  51.  
  52. Expression = "8+3^2"
  53. Result = EvalExpression(Expression)
  54. // Result = 17
  55.  
  56. Expression = "16%6"
  57. Result = EvalExpression(Expression)
  58. // Result = 4
  59.  
  60. Expression = "-5^2"
  61. Result = EvalExpression(Expression)
  62. // Result = -25
  63.  
  64. Expression = "-(5^2)"
  65. Result = EvalExpression(Expression)
  66. // Result = -25
  67.  
  68. Expression = "(-5)^2"
  69. Result = EvalExpression(Expression)
  70. // Result = 25
  71.  
  72. Expression = "±5^2"
  73. Result = EvalExpression(Expression)
  74. // Result = 25
  75.  
  76. Expression = "2/5+(2-1)"
  77. Result = EvalExpression(Expression)
  78. // Result = 1.4
  79.  
  80. Expression = "6(7-2)-1"
  81. Result = EvalExpression(Expression)
  82. // Result = 29
  83.  
  84. Expression = "5+3*(4-1)/2^2"
  85. Result = EvalExpression(Expression)
  86. // Result = 7.25
  87.  
  88. Expression = "_pi()"
  89. Result = EvalExpression(Expression)
  90. // Result = 3.1415927
  91.  
  92. Expression = "_abs(_floor(-5.3)+1)"
  93. Result = EvalExpression(Expression)
  94. // Result = 5
  95.  
  96. EvalExpression Functions
  97.  
  98. EvalExpression 1.3 supports functions.
  99.   _sin(x) returns the sine of x
  100.   _cos(x) returns the cosine of x
  101.   _tan(x) returns the tangent of x
  102.   _asin(x) returns the arc sine of x
  103.   _acos(x) returns the arc cosine of x
  104.   _atan(x) returns the arc tangent of x
  105.   _log(x) returns the base ten logarithm of x
  106.   _ln(x) returns the base e logarithm of x
  107.   _abs(x) returns the absolute value of x
  108.   _rand(x) returns a random number between 0 and x
  109.   _round(x) rounds x to the nearest whole number
  110.   _floor(x) rounds x down to a whole number
  111.   _ceil(x) rounds x up to a whole number
  112.   _pi() returns a decimal approximation of pi
  113.  
  114. EvalExpression Version History
  115.  
  116. 1.3
  117.    Added support for mathematical functions such as _sin or _abs
  118.  
  119. 1.2
  120.    Added % operand for remainders
  121.    Added ± operand for the opposite of a number
  122.    Fixed bugs with (-a)*b, (-a)^b, etc., operations
  123.  
  124. 1.1
  125.    Fixed bugs with multiple, non-nested parenthesis
  126.    Added support for a(b), (a)(b), and (a)b multiplication
  127.  
  128. 1.0
  129.    Initial release
  130.  
  131. EvalExpression Legal Info
  132.  
  133. EvalExpression is free to use and distribute.  You may use it in any application that you make, provided that I am given fair credit for EvalExpression.  If you use it in a shareware or commercial product, contact me and we can work out a deal.
  134.  
  135. EvalExpression is open source: that is, you are free to copy or modify any parts of the source code for EvalExpression, provided that you send a copy of the modified source file to me so that we can learn from each other's progress.